﻿/// <reference path="siteTypesOffice.js" />
/// <reference path="Office.js" />

Office._ExcelMask = 0x1;
Office._WordMask = 0x2;
Office._ProjectMask = 0x4;
Office._OutlookMask = 0x8;

//Definitions

Office._BindingDataChangedEvents = function (eventType) {
    this.binding = new Office._Binding(bindingType);
    this.type = eventType;
    this.startColumn = {};
    this.startRow = {};
}

Office._CustomXmlNodeEvents = function (eventType) {
    this.type = eventType;
    this.inUndoRedo = {};

    if (eventType == 'nodeDeleted') {
        this.oldNode = new Office._CustomXmlNode();
        this.oldNextSibling = new Office._CustomXmlNode();
    }

    if (eventType == 'nodeInserted') {
        this.newNode = new Office._CustomXmlNode();
       

    }
    if (eventType == 'nodeReplaced') {
        this.oldNode = new Office._CustomXmlNode();
        this.newNode = new Office._CustomXmlNode();

    }
}

Office._CustomXmlNode= function () {
    this.baseName = {};
    this.namespaceUrl = {};
    this.nodeType = {};

    this.getNodesAsync = function (xPath, callback) { };
    this.getNodeValueAsync = function (callback) { };
    this.getXmlAsync = function (callback) { };
    this.setNodeValueAsync = function (value, callback) { };
    this.setXmlAsync = function (xml,callback){};

}

Office._context_document_customXmlParts_customXmlPrefixMappings = function () {
    this.addNamespaceAsync = function (prefix, nsUri, callback) { };
    this.getNamespaceAsync = function (prefix, callback) { };
    this.getPrefixAsync = function (nsUri, callback) { };
}

Office._CustomXmlPart = function () {
    this.builtIn = {};
    this.id = {};
    this.namespaceManager = new Office._context_document_customXmlParts_customXmlPrefixMappings();
 
    this.deleteAsync = function (callback) {
    };
    this.getNodesAsync = function (xPath, callback) {

        var result = new Office._AsyncResult("getNodesAsync");
        callback(result);
    };
    this.getXmlAsync = function (callback) {
    };

    this.addHandlerAsync = function (eventType, handler, callback) {
        ///<summary> Adds and event handler to the object using the specified event type.  </summary>
        ///<param name="eventType" type="Office.EventType">The event type. For CustomXmlPartNode it can be 'nodeDeleted', 'nodeInserted' or 'nodeReplaced' </param>
        ///<param name="handler" type="function">The name of the handler </param>
        ///<param name="callback" type="function" optional="true">The optional callback method</param>


        var events = new Office._CustomXmlNodeEvents(eventType);
        handler(events);

        if (arguments.length == 1) {
            callback = asyncContext;
        }
        var result = new Office._AsyncResult("addHandlerAsync");
        callback(result);
    };

    this.removeHandlerAsync = function (eventType, options, callback) {
        ///<summary> Removes and event handler from the object using the specified event type.  </summary>
        ///<param name="eventType" type="Office.EventType">The event type. For CustomXmlPartNode it can be 'nodeDeleted', 'nodeInserted' or 'nodeReplaced' </param>
        ///<param name="options" type="Object" optional="true">
        ///    Syntax example: {handler:eventHandler}
        /// &#10;     handler: Indicates a specific handler to be removed, if not specified all handlers are removed
        /// &#10;     asyncContext: Don't forget what you were doing!
        ///</param>
        ///<param name="callback" type="function" optional="true">The optional callback method</param>
    }


}

    Office._Binding = function (bindingType) {
        ///<field type="String">Id of the Binding</field>};
        this.id = {};
       
        this.type = {};
        this.document = {};

        this.setDataAsync = function (data, options, callback) {
            ///<summary> Writes the specified data into the current selection.</summary>
            ///<param name="data" type="Object">The data to be set. Either a string or value, 2d array or TableData object</param>
            ///<param name="options" type="Object" optional="true">
            ///    Syntax example: {coercionType:Office.CoercionType.Matrix} or {coercionType: 'matrix'}
            /// &#10;     coercionType: Explicitly sets the shape of the data object. Use Office.CoercionType or text value. If not supplied is inferred from the data type.
            /// &#10;     asyncContext: Don't forget what you were doing!
            ///</param>
            ///<param name="callback" type="function" optional="true">The optional callback method</param>

        };

        this.getDataAsync = function (options, callback) {
            ///<summary> Returns the current selection.</summary>
            ///<param name="options" type="Object" optional="true">
            ///    Syntax example: {coercionType: 'matrix,'valueFormat: 'formatted', filterType:'all'}
            /// &#10;     coercionType: The expected shape of the selection. If not specified returns the bindingType shape. Use Office.CoercionType or text value.
            /// &#10;     valueFormat: Get data with or without format. Use Office.ValueFormat or text value.
            /// &#10;     filterType: Get the visible or all the data. Useful when filtering data. Use Office.FilterType or text value.
            /// &#10;     asyncContext: Don't forget what you were doing!
            ///</param>
            ///<param name="callback" type="function" optional="true">The optional callback method</param>


            if (arguments.length == 1) {
                callback = options;
            }
            var result = new Office._AsyncResult("getDataAsync");
            callback(result);
        };
   
        this.addHandlerAsync = function (eventType, handler, callback) {
            ///<summary> Adds and event handler to the object using the specified event type.  </summary>
            ///<param name="eventType" type="Office.EventType">The event type. For binding it can be 'bindingDataChanged' and 'bindingSelectionChanged' </param>
            ///<param name="handler" type="function">The name of the handler </param>
            ///<param name="callback" type="function" optional="true">The optional callback method</param>


            var events = new Office._BindingDataChangedEvents(eventType);
            handler(events);

            if (arguments.length == 1) {
                callback = asyncContext;
            }
            var result = new Office._AsyncResult("addHandlerAsync");
            callback(result);
        };

        this.removeHandlerAsync = function (eventType, options, callback) {
            ///<summary> Removes and event handler from the object using the specified event type.  </summary>
            ///<param name="eventType" type="Office.EventType">The event type. For binding can be 'bindingDataChanged' and 'bindingSelectionChanged' </param>
            ///<param name="options" type="Object" optional="true">
            ///    Syntax example: {handler:eventHandler}
            /// &#10;     handler: Indicates a specific handler to be removed, if not specified all handlers are removed
            /// &#10;     asyncContext: Don't forget what you were doing!
            ///</param>
            ///<param name="callback" type="function" optional="true">The optional callback method</param>


            var events = new Office._BindingDataChangedEvents(eventType);
            handler(events);

            if (arguments.length == 1) {
                callback = asyncContext;
            }
            var result = new Office._AsyncResult("removeHandlerAsync");
            callback(result);
        };

    
        if ((bindingType == undefined) || (bindingType == Office.BindingType.Matrix) || (bindingType == Office.BindingType.Table))
        {
            this.columnCount = {};
            this.rowCount = {};
        }
        if ((bindingType == undefined) ||  (bindingType == Office.BindingType.Table))
        {
       
            this.hasHeaders = {};
            this.addRowsAsync = function (tabledata) { };
            this.addColumnsAsync = function (tabledata) { };
            this.deleteAllDataValuesAsync = function () { };
        }

    }


    Office._AsyncResult = function (method, bindingType) {
        this.asyncContext = {};
        this.error = {};
        this.status = {};

        if ((method == "addfromSelectionAsync") || (method == "addFromNamedItemAsync") || (method == "getByIdAsync") || (method == "addFromPromptAsync")) {
            this.value = new Office._Binding(bindingType);
        }
        if (method == "getDataAsync") {
            this.value = {};
        }
        if (method == "getAllAsync") {
            this.value = new Array(new Office._Binding(bindingType));
        }
 
        if ((method == "getByNamespace")){
            this.value = new Array(new Office._CustomXmlPart());
        }
        

        if (method == "getNodesAsync") {
            this.value = new Array(new Office._CustomXmlNode());
        }
        if ((method == "XMLgetByIdAsync")|| (method == "addAsync")) {
            this.value = new Office._CustomXmlPart();

        }

    }

    Office._context_license = function () {
        this.value = {
            ///<summary>License summary.</summary>
        };
    }

    Office._context_document_settings = function () {
        this.addHandlerAsync = function (eventType, handler, callback) {
            ///<summary> Adds and event handler for the object using the specified event type. </summary>
            ///<param name="eventType" type="Office.EventType">The event type. For settings can be 'settingsChanged' </param>
            ///<param name="handler" type="function">The name of the handler </param>
            ///<param name="callback" type="function" optional="true">The optional callback method</param>
        };
        this.removeHandlerAsync = function (eventType, options, callback) {
            ///<summary> Removes and event handler for the specified event type. </summary>
            ///<param name="eventType" type="Office.EventType">The event type. For settings can be 'settingsChanged' </param>
            ///<param name="options" type="Object" optional="true">
            ///    Syntax example: {handler:eventHandler}
            /// &#10;     handler: Indicates a specific handler to be removed, if not specified all handlers are removed
            /// &#10;     asyncContext: Don't forget what you were doing!
            ///</param>
            ///<param name="callback" type="function" optional="true">The optional callback method</param>

        };

        this.get = function (settingName) {
            ///<summary>Retrieves the setting with the specified name.</summary>
            ///<param name="settingName" type="string">The name of the setting </param>
       
        };

        this.refreshAsync = function (callback) {
            ///<summary>Gets the latest version of the settings object.</summary>
            ///<param name="callback" type="function" optional="true">The optional callback method</param>
        };

        this.remove = function (settingName) {
            ///<summary>Removes the setting with the specified name.</summary>
            ///<param name="settingName" type="string">The name of the setting </param>
        };

        this.saveAsync = function (options, callback) {
            ///<summary>Saves all settings.</summary>
            ///<param name="options" type="Object" optional="true">
            ///    Syntax example: {overwriteIfStale:false}
            /// &#10;     overwriteIfStale: Indicates whether the setting will be replaced if stale.
            /// &#10;     asyncContext: Don't forget what you were doing!
            ///</param>
            ///<param name="callback" type="function" optional="true">The optional callback method</param>
        };

        this.set = function (settingName, value) {
            ///<summary>Sets a value for the setting with the specified name.</summary>
            ///<param name="settingName" type="string">The name of the setting</param>
            ///<param name="value" type="object">The value for the setting</param>
        };

    };

    Office._context_document_bindings = function () {
        this.document ={};

        this.addFromSelectionAsync = function (bindingType, options, callback) {
            ///<summary>Create a binding based on what the user's current selection.</summary>
            ///<param name="bindingType" type="Office.BindingType">The Office BindingType for the data</param>
            ///<param name="options" type="Object" optional="true">
            ///    addFromSelectionAsyncOptions- e.g. {id: "myAwesomeBinding"}
            /// &#10;     id: Everyone needs a name.
            /// &#10;     asyncContext: Don't forget what you were doing!
            ///</param>
            ///<param name="callback" type="function" optional="true">The optional callback method</param>
            if (arguments.length == 2) { callback = options; };
            var result = new Office._AsyncResult("addfromSelectionAsync", bindingType);
            callback(result);
        }
        this.addFromNamedItemAsync = function (itemName, bindingType, options, callback) {
            ///<summary>Creates a binding against a named object in the document</summary>
            ///<param name="itemName" type="String">Name of the bindable object in the document. For Example 'MyExpenses' table in Excel." </param>
            ///<param name="bindingType" type="Office.BindingType">The Office BindingType for the data</param>
            ///<param name="options" type="Object" optional="true">
            ///    Syntax example: {id: "myAwesomeBinding"}
            /// &#10;     id: Name of the binding, autogenerated if not supplied. 
            /// &#10;     asyncContext: Don't forget what you were doing!
            ///</param>
            ///<param name="callback" type="function" optional="true">The optional callback method</param>

            if (arguments.length == 3) { callback = options; };
            var result = new Office._AsyncResult("addFromNamedItemAsync", bindingType);
            callback(result);
        }
        this.getByIdAsync = function (id, callback) {
            ///<summary>Create a binding based on its Name</summary>
            ///<param name="id" type="String">The binding id</param>
            ///<param name="callback" type="function" optional="true">The optional callback method</param>
       
            var result = new Office._AsyncResult("getByIdAsync")
            callback(result);
        }
        this.getAllAsync = function (callback) {
            ///<summary>Gets and array with all the binding objects in the document.</summary>
            ///<param name="callback" type="function" optional="true">The optional callback method</param>
            var result = new Office._AsyncResult("getAllAsync")
            callback(result);
        };

        this.releaseByIdAsync = function (id, callback) {
            ///<summary>Removes the binding from the document</summary>
            ///<param name="id" type="String">The binding id</param>
            ///<param name="callback" type="function" optional="true">The optional callback method</param>
        };

    
        if (Office._ExcelMask & Office._appContext) {
            this.addFromPromptAsync = function (bindingType, options, callback) {
                ///<summary>(Excel only) Create a binding by prompting the user to make a selection on the document.</summary>
                ///<param name="bindingType" type="Office.BindingType">The Office BindingType for the data</param>
                ///<param name="options" type="Object" optional="true">
                ///    addFromPromptAsyncOptions- e.g. {promptText: "Please select data", id: "mySales"}
                /// &#10;     promptText: Greet your users with a friendly word.
                /// &#10;     asyncContext: Don't forget what you were doing!
                /// &#10;     id: Everyone needs a name.
                ///</param>
                ///<param name="callback" type="function" optional="true">The optional callback method</param>

                if (arguments.length == 2) { callback = options; };
                var result = new Office._AsyncResult("addFromPromptAsync", bindingType)
                callback(result);
           

            }
        }
    }


    Office._context_document_customXmlParts = function () {
        ///<summary>(Word Only) customXML parts </summary>
        this.addAsync = function (xml, callback) {
            ///<summary>(Word Only) Asynchronously adds a new custom XML part to a file.</summary>
            ///<param name="xml" type="String">The XML to add to the newly created custom XML part.</param>
            ///<param name="callback" type="function" optional="true">A function that is invoked when the callback returns, whose only parameter is of type AsyncResult.</param>
            var result = new Office._AsyncResult("addAsync");
            callback(result);
        };
   

        this.getByIdAsync = function (id, callback) {
            ///<summary>(Word Only) Asynchronously gets the specified custom XML part(s) by its id.</summary>
            ///<param name="id" type="String">The id of the custom XML part.</param>
            ///<param name="callback" type="function" optional="true">A function that is invoked when the callback returns, whose only parameter is of type AsyncResult.</param>
        
            var result = new Office._AsyncResult("XMLgetByIdAsync");
            callback(result);

        };

        this.getByNamespace = function (ns, callback) {
            ///<summary>(Word Only) Asynchronously gets the specified custom XML part(s) by its namespace. </summary>
            ///<param name="ns" type="String">The XML to add to the newly created custom XML part.</param>
            ///<param name="callback" type="function" optional="true">A function that is invoked when the callback returns, whose only parameter is of type AsyncResult.</param>
            var result = new Office._AsyncResult("getByNamespace");
            callback(result);
        };
    }

    Office._context_mailbox = function () {
        this.displayAppointmentForm = function (id) {
            ///<summary> Displays and appointment Form</summary>
            ///<param name="id" type="integer">The id of the form</param>
        };
    }

    Office._context_document = function () {
        this.mode ={
            ///<summary>Gets the document mode</summary>
        };
        this.url ={
            ///<summary>Gets the document URL</summary>
        };

        this.addHandlerAsync = function (eventType, handler, callback) {
            ///<summary> Adds and event handler for the specified event type. </summary>
            ///<param name="eventType" type="Office.EventType">The event type. For document can be 'DocumentDataChanged' </param>
            ///<param name="handler" type="function">The name of the handler </param>
            ///<param name="callback" type="function" optional="true">The optional callback method</param>
           
        };
        this.removeHandlerAsync = function (eventType, handler, callback) {
            ///<summary> Removes and event handler for the specified event type. </summary>
            ///<param name="eventType" type="Office.EventType">The event type. For document can be 'DocumentDataChanged' </param>
            ///<param name="handler" type="function">The name of the handler </param>
            ///<param name="callback" type="function" optional="true">The optional callback method</param>
        };
        this.bindings = new Office._context_document_bindings();
        this.settings = new Office._context_document_settings();
        this.getSelectedDataAsync = function (coercionType, options, callback) {
            ///<summary> Returns the current selection.</summary>
            ///<param name="coercionType" type="Office.CoercionType">The expected shape of the selection.</param>
            ///<param name="options" type="Object" optional="true">
            ///    Syntax example: {valueFormat: 'formatted', filterType:'all'}
            /// &#10;     valueFormat: Get data with or without format. Use Office.ValueFormat or text value.
            /// &#10;     filterType: Get the visible or all the data. Useful when filtering data. Use Office.FilterType or text value.
            /// &#10;     asyncContext: Don't forget what you were doing!
            ///</param>
            ///<param name="callback" type="function" optional="true">The optional callback method</param>
        };
        this.setSelectedDataAsync = function (data, options, callback) {
            ///<summary> Writes the specified data into the current selection.</summary>
            ///<param name="data" type="Object">The data to be set. Either a string or value, 2d array or TableData object</param>
            ///<param name="options" type="Object" optional="true">
            ///    Syntax example: {coercionType:Office.CoercionType.Matrix} or {coercionType: 'matrix'}
            /// &#10;     coercionType: Explicitly sets the shape of the data object. Use Office.CoercionType or text value. If not supplied is inferred from the data type.
            /// &#10;     asyncContext: Don't forget what you were doing!
            ///</param>
            ///<param name="callback" type="function" optional="true">The optional callback method</param>
        };

        if (Office._WordMask & Office._appContext) {
            //word-specific methods
            this.customXmlParts = new Office._context_document_customXmlParts();
            this.getAllContentAsync = function (fileType, callback) {
                ///<summary>(Word only) Get the whole document.</summary>
                ///<param name="fileType" type="Office.FileType">The format in which the file will be returned</param>
                ///<param name="callback" type="function" optional="true">The optional callback method</param>

            };
            Office.CustomXmlNodeType = {
                ///<field type="String">Element</field>
                Element: "element",
                ///<field type="String">Attribute</field>
                Attribute: "attribute",
                ///<field type="String">String/field>
                Text: "text",
                ///<field type="String">CData</field>
                Cdata: "cdata",
                ///<field type="String">ProcessingInstruction</field>
                ProcessingInstruction: "processingInstruction",
                ///<field type="String">NodeComment</field>
                NodeComment: "nodeComment",
                ///<field type="String">NodeDocument</field>
                NodeDocument: "nodeDocument"
            }
       
        }
    }




    //Eveything under Office Namepsace for all apps
    Office.context = {
  

   
    };

    Office.context.contentLanguage = {};
    Office.context.displayLanguage = {};

    Office.context.license = new Office._context_license();


    Office.userShortNamespace = function (useShortNamespace) {
        ///<summary> Indicates if  the large namespace for objects will be used or not.</summary>
        ///<param name="useShortNamespace" type="boolean"> Indicates if 'true' that the short namespace will be used</param>
  

    }

    Office.initialize = function () {
        ///<summary> This method is called after the Office framework was loaded.</summary>
    }

    // Everything under Office for all apps except Outlook
    if (!(Office._OutlookMask & Office._appContext)) {

        Office.BindingType = {
            ///<field type="String">Text based Binding</field>
            Text: "text",
            ///<field type="Matrix">Matrix based Binding</field>
            Matrix: "matrix",
            ///<field type="Table">Table based Binding</field>
            Table: "table"
        };
        Office.CoercionType = {
            ///<field type="String">Coerce as Table</field>
            Table: "table",
            ///<field type="String">Coerce as Text</field>
            Text: "text",
            ///<field type="String">Coerce as Matrix</field>
            Matrix: "matrix",
            ///<field type="String">Coerce as HTML</field>
            Html: "html",
            ///<field type="String">Coerce as Office Open XML</field>
            Ooxml: "ooxml"
        };


   
        Office.DocumentMode = {
            ///<field type="String">Document in Read Only Mode</field>
            ReadOnly: "readOnly",
            ///<field type="String">Document in Read/Write Mode</field>
            ReadWrite: "readWrite"
        };

  
    
        Office.EventType ={
            ///<field type="String">Triggers when a document level selection happens</field>
            DocumentSelectionChanged:"documentSelectionChanged",
            ///<field type="String"> Triggers when a binding level selection happens</field>
            BindingSelectionChanged:"bindingSelectionChanged",
            ///<field type="String">Triggers when a binding level data change happens</field>
            BindingDataChanged:"bindingDataChanged",
            ///<field type="String">Triggers when settings change in a co-Auth session.</field>
            SettingsChanged:"settingsChanged",
            ///<field type="String">Triggers when a customXmlPart node was deleted</field>
            NodeDeleted:"nodeDeleted",
            ///<field type="String">Triggers when a customXmlPart node was inserted</field>
            NodeInserted:"nodeInserted",
            ///<field type="String">Triggers when a customXmlPart node was replaced</field>
            NodeReplaced:"nodeReplaced"


        }
        Office.FileType = {
            ///<field type="String">Returns the file as a Base 64 encoded string</field>
            Base64: "base64",
            ///<field type="String">Returns the file as plain text</field>
            Text:"text"
        }; 

        Office.FilterType = {
            ///<field type="String">Returns all items</field>
            All: "all",
            ///<field type="String">Returns only visible items</field>
            OnlyVisible: "onlyVisible"
        };

        Office.ValueFormat = {
            ///<field type="String">Returns items with format</field>
            Formatted: "formatted",
            ///<field type="String">Returns items without format</field>
            Unformatted: "unformatted"
        };

        Office.select = function (expression, callback) {
            ///<summary> Returns a promise of an object described in the expression.</summary>
            ///<param name="expression" type="string">The object to be retrieved. Example "bindings#BindingName", retrieves a binding promise for a binding named 'BindingName'</param>
            ///<param name="callback" type="function" optional="true">The optional callback method</param>
            return(new Office._Binding("select"));
        }
    
    


    }




    //Everything under Office.context for all apps except outlook
    if (!(Office._OutlookMask & Office._appContext)) {
        ///<summary>Office Document</summary>
        Office.context.document = new Office._context_document();
    }
 

    //For Outlook
    if (Office._OutlookMask & Office._appContext) {
        Office.context.mailbox = new Office._context_mailbox();
        Office.context.roamingSettings = new Office._context_document_settings();
    

        ///<field name="this.context.application.outlookAppOm" type='Object'>The Outlook Application</field>
        ///<field name="OutlookEnums" type='Object'>The Outlook Enum</field>
        Office.OutlookEnums = new Office._OutlookEnums();
    }


    if (!!intellisense) {
        intellisense.addEventListener('statementcompletion', function (event) {
            if (event.targetName === "this" || event.target === undefined || event.target === window) return;
            event.items = event.items.filter(function (item) {
                return !(item.name && item.name.charAt(0) === "_");
            });
        });
    }
